//
// $Id$

package {

import flash.events.Event;
import flash.display.*;
import flash.geom.*;

import flash.display.DisplayObject;
import flash.display.Sprite;

import flash.utils.getTimer; // function import

import com.whirled.*;

[SWF(width="144", height="192")]
public class Charas extends Sprite
{
    public function Charas ()
    {
        _ctrl = new AvatarControl(this);

        var pack :DataPack = new DataPack(_ctrl.getDefaultDataPack());
        pack.getDisplayObjects({sheet: "Sprites"}, onReady);
    }

    protected function onReady (images :Object) :void
    {
        var source :BitmapData = Bitmap(images.sheet).bitmapData;
        _sheet = new BitmapData(24*3, 32*4);
        // Copy in and remove the background color
        _sheet.threshold(source, new Rectangle(0, 0, _sheet.width, _sheet.height), ORIGIN, "==", 0xff209c00, 0, 0xffffffff, true);

        _screen = new BitmapData(24, 32);

        var view :Bitmap = new Bitmap(_screen);
        view.scaleX = view.scaleY = 6;
        addChild(view);

        _ctrl.addEventListener(ControlEvent.APPEARANCE_CHANGED, onAppearanceChanged);
        _ctrl.addEventListener(ControlEvent.STATE_CHANGED, onStateChanged);

        _ctrl.registerStates("Default", "Running");

        onStateChanged();
        onAppearanceChanged();
    }

    protected function setWalkSpeed (speed :int) :void
    {
        _walkSpeed = 100*(600/speed);
        _ctrl.setMoveSpeed(speed);
    }

    protected function onAppearanceChanged (_:*=null) :void
    {
        var orient :Number = _ctrl.getOrientation();
        var isMoving :Boolean = _ctrl.isMoving();

        if (orient > 315 || orient < 45) {
            blit(1, 2); // S
        } else if (orient <= 135) {
            blit(1, 1); // E
        } else if (orient < 225) {
            blit(1, 0); // N
        } else if (orient <= 315) {
            blit(1, 3); // W
        }

        if (_walking != isMoving) {
            _walking = isMoving;
            if (_walking) {
                _walkStart = getTimer();
                addEventListener(Event.ENTER_FRAME, onEnterFrame);
            } else {
                removeEventListener(Event.ENTER_FRAME, onEnterFrame);
            }
        }
    }

    protected function onStateChanged (event :ControlEvent = null) :void
    {
        var state :String = (event != null) ? event.name : _ctrl.getState();
        setWalkSpeed(state == "Default" ? 300 : 600);
    }

    protected function blit (x :int, y :int) :void
    {
        if (x != _x || y != _y) {
            _screen.copyPixels(_sheet, new Rectangle(x*24, y*32, 24, 32), ORIGIN);
            _x = x;
            _y = y;
        }
    }

    protected function onEnterFrame (event :Event) :void
    {
        var elapsed :Number = getTimer() - _walkStart;
        var x :int = (int(elapsed/_walkSpeed))%4;
        if (x == 3) {
            x = 1; // Swing back around
        }
        blit(x, _y);
    }

    protected var _ctrl :AvatarControl;

    protected var _screen :BitmapData;
    protected var _x :int = -1;
    protected var _y :int = -1;

    protected var _walkStart :uint;
    protected var _walking :Boolean;
    protected var _walkSpeed :int;

    protected var _sheet :BitmapData;

    protected static const ORIGIN :Point = new Point(0, 0);
}
}
